package threadLearn.producerconsumer.v1;

import java.util.Random;
import java.util.concurrent.BlockingQueue;

/**
 * 消费者
 * @author 12609
 *
 */
public class Consumer implements Runnable{

	private BlockingQueue<String> queue;
	private static final int SLEEPTIME = 1000;
	public Consumer(BlockingQueue<String> queue) {
		this.queue = queue;
	}
	
	@Override
	public void run() {

		System.out.println(Thread.currentThread().getName()+" 正准备吃面包。。。");
		Random r = new Random();
		
		try {
			while(true) {
				String bread = queue.take();
				if(bread != null) {
					System.out.println(bread+" 正在被吃。。。");
					Thread.sleep(r.nextInt(SLEEPTIME));
				}
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
			Thread.currentThread().interrupt();
		}
		
	}

}
